package com.iweb.address.servlet;

import com.alibaba.fastjson.JSONObject;
import com.iweb.address.model.Address;
import com.iweb.address.service.AddressService;
import com.iweb.address.service.AddressServiceImpl;


import com.iweb.user.model.User;
import com.iweb.util.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "*.address")
public class AddressServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getServletPath();
        switch (path) {
            case "/selectByPage.address":
                selectByPage(req, resp);
                break;
            case "/toList.address":
                toList(req, resp);
                break;
            case "/addAddress.address":
                addAddress(req, resp);
                break;
            case "/toEdit.address":
                toEdit(req, resp);
                break;
            case "/queryByUserId.address":
                queryByUserId(req, resp);
                break;
            case "/deleteById.address":
                deleteById(req, resp);
                break;
            case "/updateAddress.address":
                updateAddress(req, resp);
                break;
            case"/queryByAddressId.address":
                queryByAddressId(req, resp);
                break;
            case"/toUpdate.address":
                toUpdate(req, resp);
                break;
            default:
                break;
        }
    }

    AddressService addressService = new AddressServiceImpl();

    //分页查询
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Object userObj = session.getAttribute("user");
        User user = (User) userObj;
        Integer userid = user.getUserid();
        String pageNo = req.getParameter("pageNo") == null ? "1" : req.getParameter("pageNo");
        String likeValue = req.getParameter("likeValue") == null ? "1" : req.getParameter("likeValue");
        PageModel pageModel = new PageModel();
        pageModel.setPageNo(Integer.valueOf(pageNo));
        pageModel.setLikeValue(likeValue);
        List<Address> addresses = addressService.selectByPage(pageModel, userid);
        pageModel.setRecordList(addresses);
        String jsonStr = JSONObject.toJSONString(pageModel);
        resp.getWriter().print(jsonStr);
    }

    //跳转到列表页面
    private void toList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/address/address_selectByPage.html").forward(req, resp);
    }

    //添加地址
    private void addAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Object userObj = session.getAttribute("user");
        User user = (User) userObj;
        Integer userid = user.getUserid();
        String userfullname = req.getParameter("userfullname");
        String goodsid = req.getParameter("goodsid");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        Address address1 = new Address(null, userid, userfullname, phone, address);
        addressService.saveOne(address1);
       resp.sendRedirect(req.getContextPath() + "/address/address_addSuccess.html?goodsid="+goodsid);
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("addressToken", "addressTokenValue");
        String userid = req.getParameter("userid");
        resp.sendRedirect(req.getContextPath() + "/address/address_toAdd.html?userid=" + userid);
    }

    //根据Id查询地址
    protected void queryByUserId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Object userObj = session.getAttribute("user");
        User user = (User) userObj;
        Integer userid = user.getUserid();
        Address address = addressService.queryByUserId(Integer.valueOf(userid));
        String jsonStr = JSONObject.toJSONString(address);
        resp.getWriter().print(jsonStr);
    }

    //删除地址
    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String addressid = req.getParameter("addressid");
        addressService.deleteById(Integer.valueOf(addressid));
        resp.sendRedirect(req.getContextPath() + "/toList.address?pageNo=1&likeValue=&deleteSuccess=1");
    }
    //修改地址
    private void updateAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Object userObj = session.getAttribute("user");
        User user = (User) userObj;
        Integer userid = user.getUserid();
        String addressid = req.getParameter("addressid");
        String userfullname = req.getParameter("userfullname");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        Address address1 = new Address(Integer.valueOf(addressid), userid, userfullname, phone, address);
        addressService.updateById(address1);
        resp.sendRedirect(req.getContextPath() + "/toList.address?pageNo=1&likeValue=&updateSuccess=1");
    }
    //去往修改地址页面
    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("addressToken", "addressTokenValue");
        String addressid = req.getParameter("addressid");
        resp.sendRedirect(req.getContextPath() + "/address/address_toUpdateAddress.html?addressid=" + addressid);
    }
    //根据地址id查询地址
    private void queryByAddressId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String addressid = req.getParameter("addressid");
        Address address = addressService.queryByAddressId(Integer.valueOf(addressid));
        String jsonStr = JSONObject.toJSONString(address);
        resp.getWriter().print(jsonStr);
    }


}
